package com.h2.common.util.email.impl;

import java.util.Arrays;
import java.util.Collection;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Required;

import com.h2.common.util.email.IEmailProvider;

/**
 * An abstract implementation of {@link com.h2.common.util.email.IEmailProvider}.
 * The default from addresses field is not necessary and can be left null.
 * 
 * @since 2.1
 */
public abstract class AEmailProvider implements IEmailProvider {
   
   private static final Logger LOG = LoggerFactory.getLogger(AEmailProvider.class);
   
   ////
   
   private String _host;
   
   private String _contentType = "text/plain";
   
   private Collection<String> _defaultFromAddresses;
   
   ////
   ////

   /* (non-Javadoc)
    * @see com.pv.sdm.util.email.IEmailProvider#send(java.lang.String,
    *    java.lang.String, java.util.Collection)
    */
   public boolean send(String subject, String message, Collection<String> to) {
      return send(subject, message, getDefaultFromAddresses(), to);
   }

   /* (non-Javadoc)
    * @see com.pv.sdm.util.email.IEmailProvider#send(java.lang.String,
    *    java.lang.String, java.util.Collection, java.util.Collection)
    */
   public boolean send(String subject, String message, Collection<String> from,
         Collection<String> to) {
      return send(subject, message, getContentType(), from, to);
   }
   
   /* (non-Javadoc)
    * @see com.pv.sdm.util.email.IEmailProvider#sendTo(java.lang.String,
    *    java.lang.String, java.lang.String[])
    */
   public boolean sendTo(String subject, String message, String... to) {
      return send(subject, message, Arrays.asList(to));
   }
   
   /* (non-Javadoc)
    * @see com.pv.sdm.util.email.IEmailProvider#send(java.lang.String,
    *    java.lang.String, java.lang.String, java.util.Collection,
    *    java.util.Collection)
    */
   public boolean send(String subject, String message, String contentType,
         Collection<String> from, Collection<String> to) {
      
      if (from == null || from.isEmpty()) {
         LOG.error("No FROM address found");
         return false;
      }
      else if (to == null || to.isEmpty()) {
         LOG.error("No TO address found");
         return false;
      }
      
      return true;
   }
   
   @Override
   public String toString() {
      return String.format("Host: [%s]", getHost());
   }
   
   /**
    * @return the host
    */
   public String getHost() {
      return _host;
   }

   /**
    * @param host the host to set
    */
   @Required
   public void setHost(String host) {
      _host = host;
   }

   /**
    * @return the contentType
    */
   public String getContentType() {
      return _contentType;
   }

   /**
    * @param contentType the contentType to set
    */
   public void setContentType(String contentType) {
      _contentType = contentType;
   }

   /**
    * @return the defaultFromAddresses
    */
   public Collection<String> getDefaultFromAddresses() {
      return _defaultFromAddresses;
   }

   /**
    * @param defaultFromAddresses the defaultFromAddresses to set
    */
   public void setDefaultFromAddresses(Collection<String> defaultFromAddresses) {
      _defaultFromAddresses = defaultFromAddresses;
   }
}
